import {Component, ViewChild} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {CalendarComponent} from "ap-angular2-fullcalendar";

@IonicPage()
@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {

    calendarOptions:Object = {
        height: 'parent',
        fixedWeekCount : false,
        defaultDate: '2016-09-12',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: [
            {
                title: 'All Day Event',
                start: '2016-09-01'
            },
            {
                title: 'Long Event',
                start: '2016-09-07',
                end: '2016-09-10'
            },
            {
                id: 999,
                title: 'Repeating Event',
                start: '2016-09-09T16:00:00'
            },
            {
                id: 999,
                title: 'Repeating Event',
                start: '2016-09-16T16:00:00'
            },
            {
                title: 'Conference',
                start: '2016-09-11',
                end: '2016-09-13'
            },
            {
                title: 'Meeting',
                start: '2016-09-12T10:30:00',
                end: '2016-09-12T12:30:00'
            },
            {
                title: 'Lunch',
                start: '2016-09-12T12:00:00'
            },
            {
                title: 'Meeting',
                start: '2016-09-12T14:30:00'
            },
            {
                title: 'Happy Hour',
                start: '2016-09-12T17:30:00'
            },
            {
                title: 'Dinner',
                start: '2016-09-12T20:00:00'
            },
            {
                title: 'Birthday Party',
                start: '2016-09-13T07:00:00'
            },
            {
                title: 'Click for Google',
                url: 'http://google.com/',
                start: '2016-09-28'
            }
        ]
    };
    @ViewChild(CalendarComponent) myCalendar: CalendarComponent;

    changeCalendarView(view) {
        this.myCalendar.fullCalendar('changeView', view);
    }

    public title: boolean[] = [
        true, false, true, false, true, true, true,
        true, false, true, false, true, true, true,
        true, false, true, false, true, true, true,
        true, false, true, false, true, true, true,
        true, false, true, false, true, true, true
    ];

    public from:string;
    public from_id:number;

    constructor(public navCtrl: NavController, public navParams: NavParams) {
        this.from=this.navParams.get('from');
        this.from_id=this.navParams.get('from_id');
    }

    ionViewDidLoad() {
        // console.log('ionViewDidLoad HomePage',this.from);
    }

    onDaySelect(day) {
        console.log(day);
    }

    changeTitle(){
        let newTitle=[];
        this.title.map((val) => {
            newTitle.push(!val)
        });
        this.title=newTitle
    }

    //todo 1.基本跳转
    goDetail(id:number) {
        if(!this.from){
            this.navCtrl.push('DetailPage',{'id':id})
        }
    }

    goBack():void{
        if(this.from){
            this.navCtrl.pop();
        }
    }

    //todo 复杂跳转
    goDetailOrBack(id:number):void{
        if(!this.from){
            this.navCtrl.push('DetailPage',{'id':id})
        }else{
            //先请求后台发生关联，然后退回到之前页面
            this.navCtrl.pop().then(()=>{
                this.navCtrl.setPages([{page:'AboutDetailPage',params:{'id':this.from_id,'u_id':id}}]);
            });
            //this.navCtrl.push(this.from);//这种情况不能用push
        }
    }
}
